package org.jeecg.modules.online.cgform.enhance.impl;

import org.jeecg.modules.online.cgform.enhance.CgformEnhanceJavaListInter;
import org.jeecg.modules.online.config.exception.BusinessException;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 列表数据查询增强（示例： 转换列表字典值的显示，例如把字典值bj转换为文本北京显示）
 */
@Component("cgformEnhanceQueryDemo")
public class CgformEnhanceQueryDemo implements CgformEnhanceJavaListInter {

	@Override
	public void execute(String tableName, List<Map<String, Object>> data) throws BusinessException {
		List<VirtualDict> dict = virtualDictData();
		for (Map<String, Object> map : data) {
			Object db = map.get("province");
			if(db==null){
				continue;
			}
			String text = dict.stream()
					.filter(p -> db.toString().equals(p.getValue()))
					.map(VirtualDict::getText)
					.findAny()
					.orElse("");
			map.put("province",text);
		}
	}

	/**
	 * 模拟数据
	 * @return
	 */
	private List<VirtualDict> virtualDictData(){
		List<VirtualDict> dict = new ArrayList<VirtualDict>();
		dict.add(new VirtualDict("bj","北京"));
		dict.add(new VirtualDict("sd","山东"));
		dict.add(new VirtualDict("ah","安徽"));
		return dict;
	}

	class VirtualDict {
		String value;
		String text;

		public VirtualDict(String value,String text){
			this.value = value;
			this.text = text;
		}

		public String getValue(){
			return value;
		}


		public String getText(){
			return text;
		}
	}
}
